package com.ds.lens.manager.scheduler;

import com.ds.lens.manager.service.ExternalEventService;
import com.ds.lens.manager.service.MuteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class Scheduler {

    @Autowired
    private MuteService muteService;

    @Autowired
    private ExternalEventService externalEventService;

    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(10);
        taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
        taskScheduler.setAwaitTerminationSeconds(30);
        return taskScheduler;
    }

    @Scheduled(cron = "${cron.mute.auto}")
    public void muteJob() {
        try {
            log.info("muteJob has been started.");
            muteService.run();
            log.info("muteJob has been ended.");
        } catch (Exception e) {
            log.error("muteJob error.", e);
        }
    }

    @Scheduled(cron = "${cron.external.event.timeout}")
    public void externalEventTimeoutJob() {
        try {
            log.info("externalEventTimeoutJob has been started.");
            externalEventService.runTasks();
            log.info("externalEventTimeoutJob has been ended.");
        } catch (Exception e) {
            log.error("externalEventTimeoutJob error.", e);
        }
    }

}



